package com.neusoft.neuiotms.dcom.service.problem.impl;

import javax.persistence.EntityNotFoundException;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcom.converter.problem.ProblemAssignmentConverter;
import com.neusoft.neuiotms.dcom.dto.problem.ProblemAssignmentDTO;
import com.neusoft.neuiotms.dcom.model.problem.ProblemAssignment;
import com.neusoft.neuiotms.dcom.repository.problem.ProblemAssignmentRepository;
import com.neusoft.neuiotms.dcom.service.problem.ProblemAssignmentService;

@Service
public class ProblemAssignmentServiceImpl implements ProblemAssignmentService {

    @Autowired
    private ProblemAssignmentRepository problemAssignmentRepository;

    @Autowired
    private ProblemAssignmentConverter problemAssignmentConverter;

    @Autowired
    private ProcessInstanceService processInstanceService;

    @Override
    @Transactional
    public ProblemAssignmentDTO create(final ProblemAssignmentDTO dto, final UserBean user) {
        this.processInstanceService.setGlobalVariable(dto.getTaskId(), "clexecutor",
                dto.getExecutorCode());

        // 查询当前workNum和TaskId下是否存在记录
        final ProblemAssignment oldModel = this.problemAssignmentRepository.findByWorkOrderNumAndTaskId(
                dto.getWorkOrderNum(), dto.getTaskId());
        if (oldModel == null) { // 如果不存在新增
            dto.setId(null);
        }

        final ProblemAssignment model = this.problemAssignmentConverter.toModel(dto);
        this.problemAssignmentRepository.save(model);

        // 完成节点
        this.processInstanceService.complete(dto.getWorkOrderNum(), dto.getTaskId(), user);

        return this.problemAssignmentConverter.toDTO(model);
    }

    @Override
    public ProblemAssignmentDTO detail(final String workOrderNum, final String taskId, final UserBean user) {
        ProblemAssignment model = null;
        try {
            if (StringUtils.isNotBlank(workOrderNum) && !"NULL".equals(taskId)) {
                model = this.problemAssignmentRepository.findByWorkOrderNumAndTaskId(workOrderNum, taskId);
                if (model == null) {
                    // 获取前一节点的TaskID
                    final String fromProcessTaskId = this.processInstanceService.getLastTaskId(taskId, user);
                    if (fromProcessTaskId == null) {
                        return new ProblemAssignmentDTO();
                    } else {
                        model = this.problemAssignmentRepository.findByWorkOrderNumAndTaskId(workOrderNum,
                                fromProcessTaskId);
                    }
                }
            } else if ("NULL".equals(taskId)) {
                model = this.problemAssignmentRepository.findByWorkOrderNum(workOrderNum);
            }
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                    new Object[] {workOrderNum },
                    new Object[] {taskId });
        }
        return this.problemAssignmentConverter.toDTO(model);
    }

    @Override
    public ProblemAssignmentDTO findByWorkOrderNumAndTaskId(final String workOrderNum, final String taskId) {
        final ProblemAssignment model =
                this.problemAssignmentRepository.findByWorkOrderNumAndTaskId(workOrderNum, taskId);
        return this.problemAssignmentConverter.toDTO(model);
    }

    @Override
    public void deleteByWorkOrderNum(final String workOrderNum) {
        this.problemAssignmentRepository.deleteByWorkOrderNum(workOrderNum);
    }

}
